package com.shujia;

import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;

public class Code4UserLogin {
    static Scanner scanner ;
    static Connection connection;
    static {
        scanner =  new Scanner(System.in);
        Properties properties = new Properties();
        try {
            properties.load(new FileReader("source/jdbc.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Class.forName(properties.getProperty("driver"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            connection = DriverManager.getConnection(
                    properties.getProperty("url"),
                    properties.getProperty("user"),
                    properties.getProperty("password")
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        run();
    }

    private static void run() {
        showPage();
        login();
    }

    private static void showPage() {
        System.out.println("****************欢迎登录世纪佳缘网****************");
    }

    private static void login() {
        System.out.println("***********用户登录界面*************");
        System.out.println("请输入用户名:");
        String loginUser = scanner.next();
        System.out.println("请输入密码:");
        String loginPasswd = scanner.next();

        // 去MySQL中验证用户信息是否正确
        try {
            Statement statement = connection.createStatement();
            String sql = "SELECT id FROM login WHERE user='"+loginUser+"'AND passwd=md5('"+loginPasswd+"')";
            System.out.println("sql:"+sql);
            ResultSet resultSet = statement.executeQuery(sql);
            if (resultSet.next()){
                inlogin();
            }
            statement.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private static void inlogin() {
        System.out.println("用户登录成功...正在跳转...");
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
